package com.iwhalecloud.bss.kite.cucc.service.job.collectiongroup.singletask;

import java.util.HashMap;
import java.util.Map;

import javax.validation.constraints.NotNull;

import org.springframework.validation.annotation.Validated;

import com.iwhalecloud.bss.kite.cucc.client.dto.batch.KiteBatchAcceptDetailDTO;
import com.ztesoft.bss.common.bo.LoginInfo;
import com.ztesoft.bss.common.bo.OrgInfo;
import com.ztesoft.bss.common.bo.StaffInfo;
import com.ztesoft.bss.common.bo.UserInfo;
import com.ztesoft.bss.common.util.ContextUtil;

/**
 * @ClassName : AbstractSingleTask
 * @Description : 收入归集批量操作单条数据处理抽象类
 * @Author : xie.zhengjun
 * @Date : 2021/5/11 16:01
 */
@Validated
public abstract class AbstractSingleTask {
    protected KiteBatchAcceptDetailDTO kiteBatchAcceptDetail;

    public AbstractSingleTask(@NotNull KiteBatchAcceptDetailDTO kiteBatchAcceptDetail) {
        this.kiteBatchAcceptDetail = kiteBatchAcceptDetail;
    }

    public final void run() {
        LoginInfo loginInfo = getLoginInfo();
        ContextUtil.setLoginInfo2LoginInfoThreadLocal(loginInfo);
        try {
            handle();
        }
        finally {
            ContextUtil.removeThreadLocal();
        }
    }

    private LoginInfo getLoginInfo() {
        LoginInfo loginInfo = new LoginInfo();
        Long userId = kiteBatchAcceptDetail.getCreateStaff();
        String orgId = kiteBatchAcceptDetail.getCreateOrgId();
        String lanId = kiteBatchAcceptDetail.getLanId();
        String postProvinceNbr = kiteBatchAcceptDetail.getProvinceId();
        String staffCode = kiteBatchAcceptDetail.getCreateStaffCode();
        String regionId = kiteBatchAcceptDetail.getRegionId();
        Long staffId = kiteBatchAcceptDetail.getCreateStaff();

        UserInfo userInfo = new UserInfo();
        userInfo.setUserCode(staffCode);
        userInfo.setUserId(userId);
        userInfo.setPostLanId(lanId);
        userInfo.setPostProvinceNbr(postProvinceNbr);
        userInfo.setPostRegionId(Long.valueOf(regionId));
        Map<String, Object> extParams = new HashMap<>();
        extParams.put("account", kiteBatchAcceptDetail.getJkStaffCode());
        extParams.put("accountId", kiteBatchAcceptDetail.getJkStaffId());
        extParams.put("KITE_SCENEINST_CARTID", String.valueOf(kiteBatchAcceptDetail.getCartId()));
        userInfo.setExtParams(extParams);

        OrgInfo orgInfo = new OrgInfo();
        orgInfo.setOrgId(Long.valueOf(orgId));

        StaffInfo staffInfo = new StaffInfo();
        staffInfo.setStaffCode(staffCode);
        staffInfo.setStaffId(staffId);

        loginInfo.setUserInfo(userInfo);
        loginInfo.setOrgInfo(orgInfo);
        loginInfo.setStaffInfo(staffInfo);
        return loginInfo;
    }

    protected abstract void handle();

}
